//
//  MyAppealListController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/30.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "MyAppealListController.h"
#import "AppealViewCell.h"
#import "AppealDetailController.h"
#import "HomeDetailFinshtViewController.h"
#import "HomeFinshtViewController.h"
@interface MyAppealListController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UILabel *centerLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;
@property (weak, nonatomic) IBOutlet BoardLabel *leftNumLabel;
@property (weak, nonatomic) IBOutlet BoardLabel *centerNumLabel;
@property (weak, nonatomic) IBOutlet BoardLabel *rightNumLabel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,assign) int page;
@property (nonatomic ,copy) NSString *status;
@end

@implementation MyAppealListController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的诉求";
    
    
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.rowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AppealViewCell" bundle:nil] forCellReuseIdentifier:@"AppealViewCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        self.page = 1;
        [self getListStatus:self.status];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self getListStatus:self.status];
    }];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.dataArray removeAllObjects];
    self.status = @"0";
    self.page = 1;
    [self getListStatus:self.status];
    self.leftLabel.hidden = NO;
    self.centerLabel.hidden = YES;
    self.rightLabel.hidden = YES;
    
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AppealViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppealViewCell" forIndexPath:indexPath];
    AppealModel *model = self.dataArray[indexPath.row];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"tou"]];
    cell.bianLabel.text = [NSString stringWithFormat:@"编号:%@",model.appeal_code];
    cell.shenLabel.text = [NSString stringWithFormat:@"申请人:%@",model.real_name];
    cell.buLabel.text = [NSString stringWithFormat:@"部门:%@",model.section_name];
    cell.typeLabel.text = [NSString stringWithFormat:@"类型:%@",model.type_name];
    cell.timeLabel.text = [self ConvertStrToTime:model.create_time];
    
    if ([model.status isEqualToString:@"4"]) {
        cell.statusLabel.text = @"已完成";
    }else if ([model.status isEqualToString:@"9"]) {
        cell.statusLabel.text = @"已驳回";
    }else{
        cell.statusLabel.text = @"待处理";
    }
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AppealModel *model = self.dataArray[indexPath.row];
    
    //    if ([self.status isEqualToString:@"0"]) {
    //        AppealDetailController *appVC = [AppealDetailController loadStroyboard:[AppealDetailController new]];
    //          appVC.model = model;
    //          appVC.hidesBottomBarWhenPushed = YES;
    //          [self.navigationController pushViewController:appVC animated:YES];
    //    }
    //
    //    if ([self.status isEqualToString:@"0"]) {
    //        AppealDetailController *appVC = [AppealDetailController loadStroyboard:[AppealDetailController new]];
    //          appVC.model = model;
    //          appVC.hidesBottomBarWhenPushed = YES;
    //          [self.navigationController pushViewController:appVC animated:YES];
    //    }
    //    if ([self.status isEqualToString:@"0"]) {
    //        AppealDetailController *appVC = [AppealDetailController loadStroyboard:[AppealDetailController new]];
    //          appVC.model = model;
    //          appVC.hidesBottomBarWhenPushed = YES;
    //          [self.navigationController pushViewController:appVC animated:YES];
    //    }
    
    
    if ([self.status isEqualToString:@"0"]) {
        AppealDetailController *appVC = [AppealDetailController loadStroyboard:[AppealDetailController new]];
        appVC.model = model;
        appVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:appVC animated:YES];
    }
    if ([self.status isEqualToString:@"1"]) {
        
        if ([model.is_appraise isEqualToString:@"0"]) {
            HomeDetailFinshtViewController *sqVC = [HomeDetailFinshtViewController loadStroyboard:[HomeDetailFinshtViewController new]];
            sqVC.appealID = model.id;
            sqVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:sqVC animated:YES];
        }else{
            
            HomeFinshtViewController *sqVC = [HomeFinshtViewController loadStroyboard:[HomeFinshtViewController new]];
            sqVC.appealID = model.id;
            sqVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:sqVC animated:YES];
            
        }//测试一  测试二
        
    }
    if ([self.status isEqualToString:@"2"]) {
        HomeDetailFinshtViewController *sqVC = [HomeDetailFinshtViewController loadStroyboard:[HomeDetailFinshtViewController new]];
        sqVC.appealID = model.id;
        sqVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:sqVC animated:YES];
    }
    
    
}


//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
    
}

- (IBAction)leftBtn:(UIButton *)sender {
    self.leftLabel.hidden = NO;
    self.centerLabel.hidden = YES;
    self.rightLabel.hidden = YES;
    [self.dataArray removeAllObjects];
    self.page = 1;
    self.status = @"0";
    [self getListStatus:self.status];
    
}

- (IBAction)centerBtn:(UIButton *)sender {
    
    self.leftLabel.hidden = YES;
    self.centerLabel.hidden = NO;
    self.rightLabel.hidden = YES;
    [self.dataArray removeAllObjects];
    self.page = 1;
    self.status = @"1";
    [self getListStatus:self.status];
    
}
- (IBAction)rightBtn:(UIButton *)sender {
    self.leftLabel.hidden = YES;
    self.centerLabel.hidden = YES;
    self.rightLabel.hidden = NO;
    [self.dataArray removeAllObjects];
    self.page = 1;
    self.status = @"2";
    [self getListStatus:self.status];
    
}

-(void) getListStatus:(NSString *)status{
    [[ToastManager shardInstance]showSystemProgress:self.view];
    
    WS(weakSelf);
    [[ApiManager ShardInstance]stffListstaff_id:[TokenManager getUserId] page:self.page status:status resultBlock:^(NSDictionary *data, NSError *error) {
        
        [[ToastManager shardInstance]hideProgress:self.view];
        NSArray *array = [AppealModel mj_objectArrayWithKeyValuesArray:data[@"rs"][@"data"]];
        NumberModel *nuModel = [NumberModel mj_objectWithKeyValues:data[@"rs"][@"nums"]];
        if ([nuModel.status_0 isEqualToString:@"0"]) {
            weakSelf.leftNumLabel.hidden = YES;
        }else{
            
            weakSelf.leftNumLabel.hidden = NO;
            weakSelf.leftNumLabel.text = nuModel.status_0;
        }
        
        if ([nuModel.status_1 isEqualToString:@"0"]) {
            weakSelf.centerNumLabel.hidden = YES;
        }else{
            
            weakSelf.centerNumLabel.hidden = NO;
            weakSelf.centerNumLabel.text = nuModel.status_1;
        }
        if ([nuModel.status_2 isEqualToString:@"0"]) {
            weakSelf.rightNumLabel.hidden = YES;
        }else{
            
            weakSelf.rightNumLabel.hidden = NO;
            weakSelf.rightNumLabel.text = nuModel.status_2;
        }
        
        if (array.count ==0) {
            weakSelf.tableView.mj_footer.alpha = 0.0;
        }else{
            weakSelf.tableView.mj_footer.alpha = 1.0;
        }
        
        [self.dataArray addObjectsFromArray:array];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        weakSelf.page ++;
    }];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
    
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed: self.dataArray?@"pg_score":@"pg_internet"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
     NSString *text = self.dataArray?@"暂无数据":@"出错啦~，点此重试";
       NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                    NSForegroundColorAttributeName: [UIColor blackColor]};
       return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


@end
